package com.sy.controller;

import com.sy.dto.AdminInfo;
import com.sy.dto.ClassTabInfo;
import com.sy.dto.Table;
import com.sy.dto.UserSession;
import com.sy.entity.ClassMsg;
import com.sy.entity.Teacher;
import com.sy.vo.AdminInfoVo;
import com.sy.vo.ChangePwdVo;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@RestController
public class AdminController extends BaseController{
    //获得管理员的信息
    @RequestMapping(value = "admin",method = RequestMethod.GET)
    public AdminInfo getAdminInfo(){
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id =  session.getId();
        return adminService.getAdminInfo(String.valueOf(id));
//        return adminService.getAdminInfo("1");
    }

    //更新管理员的信息
    @RequestMapping(value = "admin",method = RequestMethod.PUT,params = {"type=updateAdminInfo"})
    public AdminInfoVo updateAdminInfo(String name, String phone){
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id =  session.getId();
//        Integer id = 1;
        AdminInfoVo adminInfoVo = new AdminInfoVo();
        adminInfoVo.setId(String.valueOf(id));
        adminInfoVo.setName(name);
        adminInfoVo.setPhone(phone);
        adminService.updateAdminInfo(adminInfoVo);
        return adminInfoVo;
    }

    //更改管理员的密码
    @RequestMapping(value = "admin",method = RequestMethod.PUT,params = {"type=changeAdminPwd"})
    public ChangePwdVo changeAdminPwd( String oldPwd, String newPwd, String verCode){
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id =  session.getId();
//        ChangePwdVo changePwdVo = new ChangePwdVo();
//        changePwdVo.setId(String.valueOf(id));
//        changePwdVo.setOldPwd(oldPwd);
//        changePwdVo.setNewPwd(newPwd);
//        changePwdVo.setVerCode(verCode);
//        loginService.changePwd(changePwdVo);
//        return changePwdVo;
        return null;
    }


    //Author:张德帅
    //根据管理员id修改管理员信息
    @RequestMapping(value = "admin",method = RequestMethod.PUT,params = {"type=editAdminInfoById"})
    public boolean editAdminInfoById(String name, String phone){
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer pushUserId = session.getId();
//        Integer id = 1;
        return adminService.editAdminInfoById(pushUserId,name,phone);
    }

    //Author:张德帅
    //根据管理员id修改管理员密码
    @RequestMapping(value = "admin",method = RequestMethod.PUT,params = {"type=editAdminPwdById"})
    public boolean editAdminPwdById(String oldPwd, String newPwd, String newPwdAgain){
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer pushUserId = session.getId();
//        Integer id = 1;
        return adminService.editAdminPwdById(pushUserId,oldPwd,newPwd,newPwdAgain);
    }

    //Author:张德帅
    //根据分页获得相应的公告信息列表(LayUi表格格式)
    @RequestMapping(value = "notices",method = RequestMethod.GET,params = {"type=getNotices","page","limit"})
    public Table getNoticesByPage(Integer page,Integer limit){
        return adminService.getNoticesByPage(page, limit);
    }

    //Author:张德帅
    //根据分页以及关键字获得相应的公告信息列表(LayUi表格格式)
    @RequestMapping(value = "notices",method = RequestMethod.GET,params = {"type=getNotices","page","limit","keyWords"})
    public Table getNoticesByPageAndKeyWords(Integer page,Integer limit,String keyWords) throws UnsupportedEncodingException {
        keyWords = URLDecoder.decode(keyWords, "UTF-8");
        return adminService.getNoticesByPageAndKeyWords(page, limit, keyWords);
    }

    //Author:张德帅
    //添加公告信息
    @RequestMapping(value = "notices",method = RequestMethod.POST,params = {"type=addNotice"})
    public boolean addNotice(String title, String notice){
//        UserSession session = (UserSession) req.getSession().getAttribute("session");
//        Integer pushUserId = session.getId();
        Integer pushUserId = 1;
        return adminService.addNotice(title, notice, pushUserId);
    }

    //Author:张德帅
    //根据公告id数组批量删除公告信息
    @RequestMapping(value = "notices",method = RequestMethod.DELETE,params = {"type=delNoticesByIds"})
    public boolean delNoticesByIds(@RequestBody Integer[] ids){
        return adminService.delNoticesByIds(ids);
    }

    //Author:张德帅
    //根据公告id删除公告信息
    @RequestMapping(value = "notices/{id}",method = RequestMethod.DELETE,params = {"type=delNoticeById"})
    public boolean delNoticeById(@PathVariable("id") Integer id){
        return adminService.delNoticeById(id);
    }

    //Author:张德帅
    //根据公告id修改公告信息
    @RequestMapping(value = "notices/{id}",method = RequestMethod.PUT,params = {"type=editNoticeById" })
    public boolean editNoticeById(@PathVariable("id") Integer id, String title, String notice){
//        UserSession session = (UserSession) req.getSession().getAttribute("session");
//        Integer pushUserId = session.getId();
        Integer pushUserId = 1;
        return adminService.editNoticeById(id,title,notice,pushUserId);
    }

    //Author:张德帅
    //根据分页获得相应的学生信息列表(LayUi表格格式)
    @RequestMapping(value = "students",method = RequestMethod.GET,params = {"type=getStudentsInfo","page","limit"})
    public Table getStudentsInfoByPage(Integer page,Integer limit){
        return adminService.getStudentsInfoByPage(page,limit);
    }

    //Author:张德帅
    //根据分页以及关键字获得相应的学生信息列表(LayUi表格格式)
    @RequestMapping(value = "students",method = RequestMethod.GET,params = {"type=getStudentsInfo","page","limit","keyWords"})
    public Table getStudentsInfoByPageAndKeyWords(Integer page,Integer limit,String keyWords) throws UnsupportedEncodingException {
        keyWords = URLDecoder.decode(keyWords, "UTF-8");
        return adminService.getStudentsInfoByPageAndKeyWords(page,limit,keyWords);
    }

    //Author:张德帅
    //添加学生信息
    @RequestMapping(value = "students",method = RequestMethod.POST,params = {"type=addStudentInfo"})
    public boolean addStudentInfo(Integer studyno, String password, String name, Integer genderId, String phone, Integer classId){
        return adminService.addStudentInfo(studyno,password,name,genderId,phone,classId);
    }

    //Author:张德帅
    //根据学生id数组批量删除学生信息
    @RequestMapping(value = "students",method = RequestMethod.DELETE,params = {"type=delStudentsInfoByIds"})
    public boolean delStudentsInfoByIds(@RequestBody Integer[] ids){
        return adminService.delStudentsInfoByIds(ids);
    }

    //Author:张德帅
    //根据学生id删除学生信息
    @RequestMapping(value = "students/{id}",method = RequestMethod.DELETE,params = {"type=delStudentInfoById"})
    public boolean delStudentInfoById(@PathVariable("id") Integer id){
        return adminService.delStudentInfoById(id);
    }

    //Author:张德帅
    //根据学生id修改学生信息
    @RequestMapping(value = "students/{id}",method = RequestMethod.PUT,params = {"type=editStudentInfoById" })
    public boolean editStudentInfoById(@PathVariable("id") Integer id, Integer studyno, String password, String name, Integer genderId, String phone, Integer classId){
        return adminService.editStudentInfoById(id,studyno,password,name,genderId,phone,classId);
    }

    //Author:张德帅
    //获得全部老师信息列表
    @RequestMapping(value = "teachers",method = RequestMethod.GET,params = {"type=getAllTeachersInfo"})
    public List<Teacher> getAllTeachersInfo(){
        return adminService.getAllTeachersInfo();
    }

    //Author:张德帅
    //根据分页获得相应的老师信息列表(LayUi表格格式)
    @RequestMapping(value = "teachers",method = RequestMethod.GET,params = {"type=getTeachersInfo","page","limit"})
    public Table getTeachersInfoByPage(Integer page,Integer limit){
        return adminService.getTeachersInfoByPage(page,limit);
    }

    //Author:张德帅
    //根据分页以及关键字获得相应的老师信息列表(LayUi表格格式)
    @RequestMapping(value = "teachers",method = RequestMethod.GET,params = {"type=getTeachersInfo","page","limit","keyWords"})
    public Table getTeachersInfoByPageAndKeyWords(Integer page,Integer limit,String keyWords) throws UnsupportedEncodingException {
        keyWords = URLDecoder.decode(keyWords, "UTF-8");
        return adminService.getTeachersInfoByPageAndKeyWords(page,limit,keyWords);
    }

    //Author:张德帅
    //添加老师信息
    @RequestMapping(value = "teachers",method = RequestMethod.POST,params = {"type=addTeacherInfo"})
    public boolean addTeacherInfo(Integer workno, String password, String name, Integer genderId, String phone){
        return adminService.addTeacherInfo(workno,password,name,genderId,phone);
    }

    //Author:张德帅
    //根据老师id数组批量删除老师信息
    @RequestMapping(value = "teachers",method = RequestMethod.DELETE,params = {"type=delTeachersInfoByIds"})
    public boolean delTeachersInfoByIds(@RequestBody Integer[] ids){
        return adminService.delTeachersInfoByIds(ids);
    }

    //Author:张德帅
    //根据老师id删除老师信息
    @RequestMapping(value = "teachers/{id}",method = RequestMethod.DELETE,params = {"type=delTeacherInfoById"})
    public boolean delTeacherInfoById(@PathVariable("id") Integer id){
        return adminService.delTeacherInfoById(id);
    }

    //Author:张德帅
    //根据老师id修改老师信息
    @RequestMapping(value = "teachers/{id}",method = RequestMethod.PUT,params = {"type=editTeacherInfoById"})
    public boolean editTeacherInfoById(@PathVariable("id") Integer id, Integer workno, String password, String name, Integer genderId, String phone, Integer classId){
        return adminService.editTeacherInfoById(id,workno,password,name,genderId,phone);
    }

    //Author:张德帅
    //获得所有班级信息列表
    @RequestMapping(value = "classes",method = RequestMethod.GET,params = {"type=getAllClassesMsg"})
    public List<ClassTabInfo> getAllClassesMsg(){
        return adminService.getAllClassMsg();
    }

    //Author:张德帅
    //根据分页获得班级信息列表(LayUi表格格式)
    @RequestMapping(value = "classes",method = RequestMethod.GET,params = {"type=getClassesInfo","page","limit"})
    public Table getClassesInfoByPage(Integer page,Integer limit){
        return adminService.getClassesInfoByPage(page,limit);
    }

    //Author:张德帅
    //根据分页以及关键字获得班级信息列表(LayUi表格格式)
    @RequestMapping(value = "classes",method = RequestMethod.GET,params = {"type=getClassesInfo","page","limit","keyWords"})
    public Table getClassesInfoByPageAndKeyWords(Integer page, Integer limit, String keyWords) throws UnsupportedEncodingException {
        keyWords = URLDecoder.decode(keyWords, "UTF-8");
        return adminService.getClassesInfoByPageAndKeyWords(page,limit,keyWords);
    }

    //Author:张德帅
    //添加班级信息
    @RequestMapping(value = "classes",method = RequestMethod.POST,params = {"type=addClassInfo"})
    public boolean addClassInfo(String className, Integer teacherId){
        return adminService.addClassInfo(className,teacherId);
    }

    //Author:张德帅
    //根据id修改班级信息
    @RequestMapping(value = "classes/{id}",method = RequestMethod.PUT,params = {"type=editClassInfoById"})
    public boolean editClassInfoById(@PathVariable("id")Integer id, String className, Integer teacherId){
        return adminService.editClassInfoById(id,className,teacherId);
    }
}
